package com.xiaotu.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.xiaotu.constant.CrowdConstant;
import com.xiaotu.entity.Admin;
import com.xiaotu.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.security.Key;

@Controller
public class AdminHandler {
    @Autowired
    private AdminService adminService;
    /**
     * 登录
     * @param loginAcct 用户名
     * @param userPswd  用户密码
     * @param httpSession session
     * @return
     */
    @RequestMapping("/admin/do/login.html")
    public String doLogin(@RequestParam("loginAcct")String loginAcct, @RequestParam("userPswd")String userPswd, HttpSession httpSession){
        //调用service方法进行登录检查
        //这个方法能返回admin对象说明登录成功,如果账号,密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);
        httpSession.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN, admin);
        return "redirect:/admin/to/main/page.html";
    }
    /**
     * 退出登录
     * @param httpSession
     * @return
     */
    @RequestMapping("/admin/do/logout.html")
    public String doLogOut(HttpSession httpSession){
        //强制Session失效
        httpSession.invalidate();
        return "redirect:/admin/to/login/page.html";
    }

    /**
     * 获取列表跳转页面
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @param model
     * @return
     */
    @RequestMapping("/admin/get/page.html")
    public String getPageInfo(
            @RequestParam(value = "keyword",defaultValue = "")String keyword,
            @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
            @RequestParam(value = "pageSize",defaultValue = "5")Integer pageSize,
            Model model
    ){

        PageInfo<Admin> pageInfo = adminService.getPageInfo(keyword, pageNum, pageSize);
       // System.out.println(pageInfo);
        model.addAttribute(CrowdConstant.ATTR_NAME_PAGE_INFO, pageInfo);
        return "admin-page";
    }

    /**
     * 单条删除
     * @param adminId
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("/admin/remove/{adminId}.html")
    public String remove(
            @PathVariable("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword
    ){
        //执行删除
        adminService.removeAdminId(adminId);
        //跳转页面 重定向  保持删除之前的查询条件和页面值并刷新页面
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;
    }
    @RequestMapping("/admin/save.html")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/get/page.html?pageNum="+Integer.MAX_VALUE;
    }
    /**
     * 跳转修改页面
     * @param adminId
     * @param pageNum
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping("/admin/to/edit/page.html")
    public String toEditPage(@RequestParam("adminId")Integer adminId,
                             @RequestParam("pageNum")Integer pageNum,
                             @RequestParam("keyword")String keyword,
                             Model model
                             ){
        Admin admin = adminService.getAdminByAdminId(adminId);
        model.addAttribute("admin", admin);
        return "admin-edit";
    }

    /**
     * 修改
     * @param admin
     * @param pageNum
     * @param keyword
     * @return
     */
    @RequestMapping("/admin/update.html")
    public String update(
            Admin admin,
            @RequestParam("pageNum")Integer pageNum,
            @RequestParam("keyword")String keyword){
        System.out.println(admin);
        adminService.update(admin);
        return "redirect:/admin/get/page.html?pageNum="+pageNum+"&keyword="+keyword;

    }

}
